package pm.devices.drives;

import pm.devices.drives.Drive;
import pm.devices.drives.DriveType;

import java.util.ArrayList;

/**
 * Created by fat3kb
 * Date: 02.04.2008
 * Time: 0:22:25
 */
public class DrivesUtil {

	/**
	 * The drive type cannot be determined.
	 */
	public static final int DRIVE_UNKNOWN = 0;

	/**
	 * The root directory does not exist.
	 */
	public static final int DRIVE_NO_ROOT_DIR = 1;

	/**
	 * The disk can be removed from the drive.
	 */
	public static final int DRIVE_REMOVABLE = 2;

	/**
	 * The disk cannot be removed from the drive.
	 */
	public static final int DRIVE_FIXED = 3;

	/**
	 * The drive is a remote (network) drive.
	 */
	public static final int DRIVE_REMOTE = 4;

	/**
	 * The drive is a CD-ROM drive.
	 */
	public static final int DRIVE_CDROM = 5;

	/**
	 * The drive is a RAM disk.
	 */
	public static final int DRIVE_RAMDISK = 6;

	/**
	 * Retrieves the system's logical drives.
	 *
	 * @return an array of <CODE>String</CODE> objects that contains the logical
	 *         drive names in the form of <CODE><B>x:\</B></CODE> where <CODE><B>x</B></CODE> denotes the drive letter
	 */
	public static native String[] getLogicalDrives();

	/**
	 * Retrieves the specified drive's type.
	 *
	 * @param drive a <CODE>String</CODE> that specifies the root directory
	 *              of the disk to return the drive type information about. If <CODE>drive</CODE> is
	 *              <CODE>null</CODE> the method uses the root of the current directory.
	 * @return an <CODE>int</CODE> that specifies the drive type
	 * @see #getLogicalDrives
	 * @see #DRIVE_UNKNOWN
	 * @see #DRIVE_NO_ROOT_DIR
	 * @see #DRIVE_REMOVABLE
	 * @see #DRIVE_FIXED
	 * @see #DRIVE_REMOTE
	 * @see #DRIVE_CDROM
	 * @see #DRIVE_RAMDISK
	 */
	public static native int getDriveType(String drive);

	public static Drive[] getDrives() {
		return getDrives(null);
	}

	public static Drive[] getDrives(String[] drives) {
		if (drives == null) {
			drives = DrivesUtil.getLogicalDrives();
		}

		ArrayList<Drive> devicesList = new ArrayList<Drive>();

		for (int i = 0; i < drives.length; i++) {
			switch (DrivesUtil.getDriveType(drives[i] + ":\\")) {
				case DrivesUtil.DRIVE_CDROM:
					devicesList.add(new Drive(drives[i], DriveType.CD_DVD));
					break;
				case DrivesUtil.DRIVE_REMOVABLE:
					devicesList.add(new Drive(drives[i], DriveType.REMOVABLE));
					break;
				default:
					devicesList.add(new Drive(drives[i], null));
			}
		}

		return devicesList.toArray(new Drive[devicesList.size()]);
	}
}
